package com.gomtel.smartdevice.ui.dialog;

import android.content.Context;
import android.support.annotation.StyleRes;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;

import com.gomtel.smartdevice.R;
import com.gomtel.smartdevice.base.WatchBaseDialog;
import com.gomtel.smartdevice.moudle.event.CommonEvent;
import com.gomtel.smartdevice.moudle.event.EventCode;
import com.gomtel.smartdevice.moudle.rxtools.EventBusUtil;
import com.gomtel.smartdevice.utils.LogUtil;
import com.gomtel.smartdevice.utils.SystemUtil;

import butterknife.BindView;


/**
 * Created by Administrator on 2018/3/21.
 */

public class DatepickerDialog extends WatchBaseDialog implements View.OnClickListener {

    private Context context;
    private String currentDate;
    @BindView(R.id.datepicker)
    DatePicker datepicker;
    @BindView(R.id.btn_cancle)
    Button btn_cancle;
    @BindView(R.id.btn_commit)
    Button btn_commit;
    private int year;
    private int month;
    private int day;
    private String[] split;

    protected DatepickerDialog(Context context, @StyleRes int themeResId, String currentDate) {
        super(context, themeResId);
        this.context = context;
        this.currentDate = currentDate;
    }

    @Override
    protected int getLayout() {
        return R.layout.dialog_date_paicker;
    }

    @Override
    protected void initClass() {

    }

    @Override
    protected void initData() {
        split = currentDate.split("-");
        year = Integer.valueOf(split[0]);
        month = Integer.valueOf(split[1]) - 1;
        day = Integer.valueOf(split[2]);
    }

    @Override
    protected void initView() {
        datepicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                DatepickerDialog.this.year = year;
                DatepickerDialog.this.month = monthOfYear;
                DatepickerDialog.this.day = dayOfMonth;
            }
        });
    }

    @Override
    protected void initListener() {
        btn_cancle.setOnClickListener(this);
        btn_commit.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_cancle:
                dismiss();
                break;
            case R.id.btn_commit:
                StringBuffer append = new StringBuffer().append(year).append("-").append(format(month + 1)).append("-").append(format(day));
                EventBusUtil.sendEvent(new CommonEvent(EventCode.SELECTTIME, append.toString()));
                dismiss();
                break;
        }
    }

    private String format(int i){
        String format = "";
        if (i<10){
            format = new StringBuffer().append("0").append(i).toString();
        }else {
            format = String.valueOf(i);
        }
       return format;
    }


    @Override
    protected void onDismissListener() {
        super.onDismissListener();
        SystemUtil.windowToLight(context);
    }


}
